//
//  MusicPlayViewController.m
//  LO_MUSIC
//
//  Created by 胡林 on 16/2/26.
//  Copyright © 2016年 杨少锋. All rights reserved.
//

#import "MusicPlayViewController.h"
#import "lyricModel.h"
#import "AudioPlayer.h"
#import "LyricTool.h"
@interface MusicPlayViewController ()<UITableViewDataSource,UITableViewDelegate,AudioPlayerDelegate>
{
   
    
}

@property (weak, nonatomic) IBOutlet UIImageView *imageView;//需要转动的图片

@property (weak, nonatomic) IBOutlet UITableView *lyricTable;//歌词

@property (weak, nonatomic) IBOutlet UILabel *beginLabel;//开始时间
@property (weak, nonatomic) IBOutlet UILabel *endLable;//结束时间

@property (weak, nonatomic) IBOutlet UISlider *timeSlider;//时间滑块
//歌词内容
@property(nonatomic , strong)NSMutableArray * lyricArray;

@property(nonatomic , strong)AudioPlayer * audioPlaying;



@end

@implementation MusicPlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
//整页滑动
//    self.lyricTable.pagingEnabled = YES;
    
      self.playModel = self.playArray[self.num];
    //初始化
    self.lyricArray = [NSMutableArray array];
    [self musicPlaying];
    //绑定进度条
    [self.timeSlider addTarget:self action:@selector(timeSliderAction:) forControlEvents:(UIControlEventValueChanged)];
}

//时间进度条方法

-(void)timeSliderAction:(UISlider *)sender{
    [[AudioPlayer shareAudioPlayer] seekoWithTime:sender.value];
}
#pragma mark - 每一秒都会走
-(void)playWithTime:(CGFloat)time{
    // 修改时间进度条最大值
    self.timeSlider.maximumValue = [self.playModel.duration floatValue] / 1000;
//    self.endLable.text = [self timeStrFromTime:[self.playModel.duration floatValue] / 1000];
    //更新进度条
    self.timeSlider.value = time;
    //图片旋转
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI / 180);
    // 正在播放时间
    self.beginLabel.text = [self timeStrFromTime:time];
    //剩余时间
    self.endLable.text =[self timeStrFromTime:([self.playModel.duration integerValue] / 1000 - time)];
    
    //现在应该执行那句
    NSInteger index = [[LyricTool shareLyricTool] indexForTime:time];
    if (index >= 0) {
        self.singingIndex = [NSIndexPath indexPathForRow:index inSection:0];
        [self.lyricTable reloadData];
    }
    //获取到当前播放歌词的下标
    //歌词越界
    if (index > 0 && index < [[LyricTool shareLyricTool].modelArr count]) {
    [self.lyricTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:(UITableViewScrollPositionMiddle) animated:YES];
    }
}

/**
 *  时间转字符串
 *
 *  @param time 秒数
 *
 *  @return 返回的字符串
 */
//计算几分几秒z
- (NSString *)timeStrFromTime:(float)time
{//几分
    int minutes = time / 60;
    //几秒
    int second = (int)time % 60;
    
    return [NSString stringWithFormat:@"%02d:%02d", minutes, second];
}
//每组多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [LyricTool shareLyricTool].modelArr.count;
}


-(void)musicPlaying{
    //播放音乐

    [[AudioPlayer shareAudioPlayer] plauWithurl:self.playModel.mp3Url];

    // 修改圆角
    self.imageView.layer.cornerRadius = 110;
    self.imageView.layer.masksToBounds = YES;
    //   //传图片
    self.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.playModel.picUrl]]];
    //title
    self.title = self.playModel.name;
    //代理
    self.lyricTable.delegate = self;
    self.lyricTable.dataSource = self;
    self.audioPlaying.delegate = self;
    //歌词
    LyricTool * lytool = [LyricTool shareLyricTool];
    [lytool lyerWithStr:self.playModel.lyric];
    
}
//播放结束，继续播放下一首
-(void)playFinish{
    [self textSongAction:nil];
}
#pragma mark - 懒加载
-(AudioPlayer *)audioPlaying{
    if (!_audioPlaying) {
        _audioPlaying = [AudioPlayer shareAudioPlayer];
    }return _audioPlaying;
}



#pragma mark cell 显示歌词
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"lyricTable"];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"lyricTable"];
    }
    if (self.singingIndex == indexPath) {
        cell.textLabel.textColor = [UIColor greenColor];
    }else{
        cell.textLabel.textColor = [UIColor blackColor];
    }
    
    LyricTool * model =[LyricTool shareLyricTool].modelArr[indexPath.row];
    cell.textLabel.text = model.lyric;
    
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    return cell;
}

#pragma mark - 上一首
- (IBAction)upSongAction:(id)sender {
 self.num--;
    if (self.num > 0) {
       self.playModel= self.playArray[self.num ];

        [self musicPlaying];


    }

  [self.lyricTable reloadData];  
}
#pragma mark - 下一首
- (IBAction)textSongAction:(id)sender {
    self.num++;
    if (self.num > 0) {
        self.playModel= self.playArray[self.num ];

        [self musicPlaying];

    }
    [self.lyricTable reloadData];

}
#pragma mark - 音量大小
- (IBAction)volumeAction:(UISlider *)sender {
    [AudioPlayer shareAudioPlayer].volume = sender.value;
}

#pragma mark - 暂停/播放
- (IBAction)stopAction:(id)sender {
    [[AudioPlayer shareAudioPlayer] playOrPause];
    
    
    
}



#pragma mark - 停止
- (IBAction)orderAction:(id)sender {
    [[AudioPlayer shareAudioPlayer] stop];
}
#pragma mark - 随机
- (IBAction)randomAction:(id)sender {
}
#pragma mark - 单曲

- (IBAction)singleAction:(id)sender {
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
